package me.jiatao.wan.ui.main

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.hi.dhl.binding.viewbind
import dagger.hilt.android.AndroidEntryPoint
import me.jiatao.wan.R
import me.jiatao.wan.databinding.ActivityMainBinding
import me.jiatao.wan.ui.fragment.MainFragmentFactory
import me.jiatao.wan.ui.main.home.HomeFragment
import me.jiatao.wan.ui.main.mine.MineFragment
import me.jiatao.wan.ui.main.project.ProjectFragment
import me.jiatao.wan.ui.main.system.SystemFragment
import me.jiatao.wan.ui.main.wechat.WechatFragment

/**
 * @Author: taojia
 * @CreateDate: 2021/8/24 1:15 下午
 * @Version: 1.0
 * @Description:
 */

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    private lateinit var mActivity: MainActivity

    private val mBinding: ActivityMainBinding by viewbind()
    private val mainFragmentFactory = MainFragmentFactory()
    private var currentSelectedMenuId = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        supportFragmentManager.fragmentFactory = mainFragmentFactory
        super.onCreate(savedInstanceState)
        mActivity = this


        val selectedItemId = savedInstanceState?.getInt("INDEX")
        if (selectedItemId == null) {
            initUI(R.id.menu_nav_home)
        } else {
            initUI(selectedItemId)
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putInt("INDEX", mBinding.bottomNavigationView.selectedItemId)
    }

    /**
     * 初始化UI
     */
    private fun initUI(selectedItemId: Int) {
        switchFragment(selectedItemId)
        mBinding.bottomNavigationView.setOnItemSelectedListener { menuItem ->
            val menuItemID = menuItem.itemId
            /**
             * 过滤重复点击
             */
            if (currentSelectedMenuId == menuItemID) {
                return@setOnItemSelectedListener false
            }
            currentSelectedMenuId = menuItemID

            return@setOnItemSelectedListener switchFragment(menuItemID)
        }
    }

    /**
     * 切换Fragment
     */
    private fun switchFragment(menuItemID: Int): Boolean {
        val transaction = supportFragmentManager.beginTransaction()
        hideFragments(transaction)
        val res = when (menuItemID) {
            R.id.menu_nav_home -> {
                showFragment<HomeFragment>(transaction)
                true
            }
            R.id.menu_nav_project -> {
                showFragment<ProjectFragment>(transaction)
                true
            }
            R.id.menu_nav_wechat -> {
                showFragment<WechatFragment>(transaction)
                true
            }
            R.id.menu_nav_system -> {
                showFragment<SystemFragment>(transaction)
                true
            }
            R.id.menu_nav_mine -> {
                showFragment<MineFragment>(transaction)
                true
            }
            else -> false
        }
        transaction.commit()
        return res
    }

    /**
     * 添加/显示fragment
     */
    private inline fun <reified F : Fragment> showFragment(
        transaction: FragmentTransaction,
    ) {
        val className = F::class.java.name;
        var fragment = supportFragmentManager.findFragmentByTag(className)
        if (fragment == null) {
            fragment = mainFragmentFactory.instantiate(mActivity.classLoader, className)
            transaction.add(R.id.fragmentContainerView, fragment, className)
        } else {
            transaction.show(fragment)
        }
        transaction.setPrimaryNavigationFragment(fragment)
    }

    /**
     * 隐藏正在显示的Fragment
     */
    private fun hideFragments(transaction: FragmentTransaction) {
        /**
         * 获取当前正在显示的fragment
         * 需要调用transaction.setPrimaryNavigationFragment(Fragment)
         * */
        supportFragmentManager.primaryNavigationFragment?.let { fragment ->
            transaction.hide(fragment)
        }
    }
}